package com.topcent.tioxyc.controller;

import com.topcent.tioxyc.constants.RabbitConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 陈百万
 * @date: 2025/10/10 15:41
 * @Description: 收款追踪表项目逻辑
 */
@Slf4j
@RestController
public class CcPaymentTrackingSheetController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 常量（保持不变）
    private static final String DATA_CREATE = "data_create";
    private static final String DATA_UPDATE = "data_update";
    private static final String SECRET = "m0xLhBCTpqNe9SDSlNMv7Hdk";

    // ==========客户预存款核销单==========
    @PostMapping("/PaymentTracking")
    public String paymentTracking(@RequestParam("nonce") String nonce,
                                  @RequestParam("timestamp") String timestamp,
                                  @RequestHeader("x-jdy-signature") String signature,
                                  @RequestBody String payload) {
        // 1. 签名校验（保持不变）
        if (!validateSignature(nonce, payload, timestamp, signature)) {
            log.warn("签名校验失败，nonce:{}", nonce);
            return "fail";
        }

        // 将 payload 发送到 RabbitMQ 队列
        rabbitTemplate.convertAndSend(RabbitConsts.PAYMENTTRACKING, payload);

        // 3. 快速返回，避免超时
        return "success";
    }

    // ========== 原有方法（保持不变） ==========
    private boolean validateSignature(String nonce, String payload, String timestamp, String signature) {
        String calculatedSignature = getSignature(nonce, payload, timestamp);
        return calculatedSignature.equals(signature);
    }

    private String getSignature(String nonce, String payload, String timestamp) {
        return DigestUtils.sha1Hex(nonce + ":" + payload + ":" + CcPaymentTrackingSheetController.SECRET + ":" + timestamp);
    }


    // ========== 简道云销售订单完成 ==========
    @PostMapping("/SalesReceiptTracking")
    public String SalesReceiptTracking(@RequestParam("nonce") String nonce,
                                  @RequestParam("timestamp") String timestamp,
                                  @RequestHeader("x-jdy-signature") String signature,
                                  @RequestBody String payload) {
        // 1. 签名校验（保持不变）
        if (!validateSignature(nonce, payload, timestamp, signature)) {
            log.warn("签名校验失败，nonce:{}", nonce);
            return "fail";
        }

        // 将 payload 发送到 RabbitMQ 队列
        rabbitTemplate.convertAndSend(RabbitConsts.SALESRECEIPTTRACKING, payload);

        // 3. 快速返回，避免超时
        return "success";
    }




    // ========== 收款单数据 ==========
    @PostMapping("/skdTracking")
    public String skdTracking(@RequestParam("nonce") String nonce,
                                       @RequestParam("timestamp") String timestamp,
                                       @RequestHeader("x-jdy-signature") String signature,
                                       @RequestBody String payload) {
        // 1. 签名校验（保持不变）
        if (!validateSignature(nonce, payload, timestamp, signature)) {
            log.warn("签名校验失败，nonce:{}", nonce);
            return "fail";
        }

        // 将 payload 发送到 RabbitMQ 队列
        rabbitTemplate.convertAndSend(RabbitConsts.SKDTracking, payload);

        // 3. 快速返回，避免超时
        return "success";
    }
}
